package cn.cf.kxx.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.LinkedHashMap;
import java.util.List;

import cn.cf.kxx.R;
import cn.cf.kxx.model.Group;
import cn.cf.kxx.model.Valve;

public class GridGroupValveAdapter extends BaseAdapter {
    private Context mContext;
    private List<Valve> mDataList;

    LinkedHashMap<Integer,Valve> selectMap = new LinkedHashMap<>();

    public GridGroupValveAdapter(Context context, List<Valve> dataList) {
        mContext = context;
        mDataList = dataList;
    }

    public void setmDataList(List<Valve> mDataList) {
        this.mDataList = mDataList;
    }

    @Override
    public int getCount() {
        return mDataList.size();
    }

    @Override
    public Object getItem(int position) {
        return mDataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.gourp_valve_item, parent, false);
            holder = new ViewHolder();
            holder.fm1 = convertView.findViewById(R.id.fm1);
            holder.fm2 = convertView.findViewById(R.id.fm2);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        Valve item = mDataList.get(position);

        holder.fm1.setText(item.getFmBh()+"号A阀门");
        holder.fm2.setText(item.getFmBh()+"号B阀门");

        if(!TextUtils.isEmpty(item.getSelect())){
            holder.fm1.setBackgroundDrawable(mContext.getDrawable(R.drawable.bg_circle_group_valve_sel));
        }else{
            holder.fm1.setBackgroundDrawable(mContext.getDrawable(R.drawable.bg_circle_group_valve));
        }

        if(!TextUtils.isEmpty(item.getSelect1())){
            holder.fm2.setBackgroundDrawable(mContext.getDrawable(R.drawable.bg_circle_group_valve_sel));
        }else{
            holder.fm2.setBackgroundDrawable(mContext.getDrawable(R.drawable.bg_circle_group_valve));
        }


        holder.fm1.setOnClickListener(v -> {
            if(TextUtils.isEmpty(item.getSelect())){
                item.setSelect("select");
            }else{
                item.setSelect("");
            }
            this.notifyDataSetChanged();
        });
        holder.fm2.setOnClickListener(v -> {
            if(TextUtils.isEmpty(item.getSelect1())){
                item.setSelect1("select");
            }else{
                item.setSelect1("");

            }
            this.notifyDataSetChanged();
        });

        if(TextUtils.isEmpty(item.getSelect1()) && TextUtils.isEmpty(item.getSelect())){
            selectMap.remove(item.getFmBh());
        }else{
            selectMap.put(item.getFmBh(),item);
        }

        return convertView;
    }

    public LinkedHashMap<Integer, Valve> getSelectMap() {
        return selectMap;
    }


    class ViewHolder {
        TextView fm1;
        TextView fm2;
    }
}
